/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * description:  生成工程实现类
 */
public class ProjectTempManager extends SupperTemplateManager {

	@SuppressWarnings("unchecked")
	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		String jarPath = entity.getPackPath();
		String projectName = entity.getProjectName();
		String sysName = entity.getSysEname();
		String xmlPath = entity.getFilePath()+ "/"+entity.getProjectName();
		FileOperate.getInstance().newCreateFolder(xmlPath + "/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/resources/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/java/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("projectName", projectName);
		try {
			data.put("sysName", GZipUtil.toUpperFirst(sysName));
			String result = FreemarkerHelper.instance().getTemplateStr("xml/pom.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/pom.xml", result);

			data.put("serverPort", entity.getParam("serverPort"));
			result = FreemarkerHelper.instance().getTemplateStr("project/application.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/src/main/resources/application.properties", result);

			result = FreemarkerHelper.instance().getTemplateStr("project/cache-conf.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/src/main/resources/cache-conf.properties", result);

			result = FreemarkerHelper.instance().getTemplateStr("project/logback-spring.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/src/main/resources/logback-spring.xml", result);

			FileOperate.getInstance().newCreateFolder(xmlPath + "/src/main/resources/redis/");
			result = FreemarkerHelper.instance().getTemplateStr("project/redis-config.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/src/main/resources/redis/redis-config.properties", result);

			xmlPath = entity.getFilePath() + "/"+entity.getProjectName()+ "/src/main/java/" + jarPath.replace(".", "/");
			FileOperate.getInstance().newCreateFolder(xmlPath+"/tomcat");
			result = FreemarkerHelper.instance().getTemplateStr("project/FilterConfiguration.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/tomcat/FilterConfiguration.java", result);

			result = FreemarkerHelper.instance().getTemplateStr("project/WebServerConfiguration.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/tomcat/WebServerConfiguration.java", result);

			result = FreemarkerHelper.instance().getTemplateStr("project/SysNameServerApplication.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "/"+GZipUtil.toUpperFirst(sysName)+"ServerApplication.java", result);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成工程文件失败", e);
		}
		return mark;
	}
}
